package com.book.book.service;

import com.book.book.NovelSiteEnum;
import com.book.common.utils.NovelSpiderUtil;
import com.book.book.entity.Novel;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/***
 * @Author Lin
 * @Date Create By 12:58 2017/10/7
 * @Description 笔下文学的书籍列表的抓取方法
 */
public class BXWX9NovelSpider extends AbstractNovelSpider {

    public List<Novel> getNovel(String url) {
        return getNovel(url, MAX_TRY_TIMES);
    }

    @Override
    public List<Novel> getNovel(String url, Integer maxTryTimes) {
        List<Novel> novels = new ArrayList<>();
        try {
            Elements trs = super.getTr(url,maxTryTimes);
            for (int index = 1, size = trs.size(); index < size; index++) {
                Element tr = trs.get(index);
                Elements tds = tr.getElementsByTag("td");
                Novel novel = new Novel();
                novel.setName(tds.first().text());
                String novelUrl = tds.first().getElementsByTag("a").first()
                        .absUrl("href").replace("binfo", "b").replace(".htm", "/");
                novel.setUrl(novelUrl);
                novel.setLastUpdateChapter(tds.get(1).text());
                novel.setLastUpdateChapterUrl(tds.get(1).getElementsByTag("a").first()
                        .absUrl("href"));
                novel.setAuthor(tds.get(2).text());
                novel.setLastUpdateTime(NovelSpiderUtil.getDate(tds.get(4).text(), "yy-MM-dd"));
                novel.setStatus(NovelSpiderUtil.getNovelStatus(tds.get(5).text()));
                novel.setPlatformId(NovelSiteEnum.getEnumByUrl(url).getId());
                novels.add(novel);

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return novels;
    }

}
